package com.zy.service.bs.impl;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.zy.dao.bs.CooperationDao;
import com.zy.entity.enums.StatusEnum;
import com.zy.entity.bs.Cooperation;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.pojo.query.bs.CooperationQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.CooperationService;
import com.zy.service.fileutil.FileUploadService;
import com.zy.utils.DateUtil;

@Service("cooperationService")
public class CooperationServiceImpl extends BaseServiceImpl<Cooperation> implements CooperationService {

	@Autowired
	private CooperationDao cooperationDao;
	@Autowired
	private FileUploadService fileUploadService;
	@Value("${fileUploadPath}")
	private String fileUploadPath;

	@Override
	public PageVo pageCooperation(CooperationQuery cooperationQuery, Integer start, Integer limit) {
		List<Cooperation> cooperations = this.cooperationDao.listCooperation(cooperationQuery, start, limit);
		if(!cooperations.isEmpty()){
			for (Cooperation cooperation : cooperations) {
				if(cooperation.getCooperationImg()!=null){
					cooperation.setCooperationImgPath(fileUploadPath + cooperation.getCooperationImg());
				}
			}
		}
		return new PageVo(start, limit, this.cooperationDao.countCooperation(cooperationQuery), cooperations);
	}

	@Override
	public void saveCooperation(Cooperation cooperation,List<MultipartFile> cooperationImgs) {
		long time = DateUtil.getTime();
		if(cooperationImgs.isEmpty()){
			throw new ServiceException("合作、赞助商图片不能为空！");
		}
		String coopImg;
		try {
			coopImg = fileUploadService.multipleUpload(cooperationImgs, "fileUploadPath");
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("合作、赞助商图片上传失败！");
		}
		cooperation.setCooperationImg(coopImg);
		cooperation.setCreateTime(time);
		cooperation.setModifyTime(time);
		cooperation.setStatus(StatusEnum.NORMAL.getValue());
		this.cooperationDao.save(cooperation);
	}
	
	@Override
	public void updateCooperation(Cooperation cooperation,List<MultipartFile> cooperationImg) {
		if(!cooperationImg.isEmpty()){
			Cooperation cooperation2 = this.cooperationDao.getByPrimaryKey(cooperation.getId());
			try {
				fileUploadService.deleteImage(cooperation2.getCooperationImg());
			} catch (IOException e1) {
				e1.printStackTrace();
				throw new ServiceException(fileUploadPath+"未找到图片");
			}
			
			String fileName;
			try {
				fileName = fileUploadService.multipleUpload(cooperationImg, "fileUploadPath");
			} catch (IOException e) {
				e.printStackTrace();
				throw new ServiceException("合作、赞助商图片修改失败！");
			}
			cooperation.setCooperationImg(fileName);
			
		}
		cooperation.setModifyTime(DateUtil.getTime());
		this.cooperationDao.updateByPrimaryKeySelective(cooperation);
	}

	@Override
	public void removeCooperation(long id) {
		try {
			Cooperation cooperation2 = this.cooperationDao.getByPrimaryKey(id);
			this.cooperationDao.removeByPrimaryKey(id);
			try {
				fileUploadService.deleteImage(cooperation2.getCooperationImg());
			} catch (IOException e1) {
				e1.printStackTrace();
				throw new ServiceException(fileUploadPath+"未找到图片");
			}
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Cooperation cooperation = new Cooperation();
			cooperation.setId(id);
			cooperation.setModifyTime(DateUtil.getTime());
			cooperation.setStatus(StatusEnum.DELETE.getValue());
			this.cooperationDao.updateByPrimaryKeySelective(cooperation);
		}
	}

	@Override
	public void recover(long id) {
		Cooperation cooperation = new Cooperation();
		cooperation.setId(id);
		cooperation.setModifyTime(DateUtil.getTime());
		cooperation.setStatus(StatusEnum.NORMAL.getValue());
		this.cooperationDao.updateByPrimaryKeySelective(cooperation);
	}

}
